#include "vedio.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
void*send_data(void*arg)
{  //1.创建tcp
    printf("qqqqqqq\n");
   int vedio_sockfd=socket(AF_INET,SOCK_STREAM,0);
   if(vedio_sockfd<0)
     { 
         printf("vedio_sockfd error\n");
         exit(1);
 
     }
    printf("wwww\n");
   struct sockaddr_in vedio_server;
   memset(&vedio_server,0,sizeof(vedio_server));
   vedio_server.sin_family=AF_INET;
   vedio_server.sin_port=htons(VEDIOPORT);
   vedio_server.sin_addr.s_addr=inet_addr(VEDIOIP);


  //2.链接
  if(connect(vedio_sockfd,(const struct sockaddr*)&vedio_server,sizeof(vedio_server))<0)
  {
    printf("connect error");
    exit(1);

  }
  
   printf("1111111\n");
  //3.创建udp
  int ali_sockfd=socket(AF_INET,SOCK_DGRAM,0);
  if(ali_sockfd<0)
    { 
        printf("ali_sockfd error");
        exit(1);

    }
    int port=*(int*)arg;
    struct sockaddr_in ali_server;
    ali_server.sin_family=AF_INET;
    ali_server.sin_port=htons(port);
    ali_server.sin_addr.s_addr=inet_addr(ALIIP);
   
   printf("22222\n");
 //阅读mjpg-streamer源码
 //1.发送视频流请求   "GET /?action=stream"
 //2.验证用户信息，发送2字节数据即可
 //3.收到报头信息，没用不做处理
 //4.收到关于视频帧大小的报头，解析
 //5.收到视频数据
 //6.使用sendto 视频数据发送云服务器
 //7.接收不重要信息 <1024



 //1.
    char*buffer=(char*)malloc(VEDIOLEN);//堆上申请空间保存视频数据最大60k
    memset(buffer,0,VEDIOLEN);
    strcpy(buffer,"GET /?action=stream\n");
    if(send(vedio_sockfd,buffer,strlen(buffer),0)<0)
       {
          printf("send error");
          exit(1);
       }
  //2.  
  if(send(vedio_sockfd,"x\n",2,0)<0)
  {
     printf("send error");
     //exit(1);
  }


  //3.
       memset(buffer,0,VEDIOLEN);
       if(recv(vedio_sockfd,buffer,VEDIOLEN,0)<0)
       {
        printf("recv error");
        exit(1);

       }

  //4.
  char*begin,*end;
  char mem[10];
    while(1)
    {

        memset(buffer,0,VEDIOLEN);
        int recvsize=recv(vedio_sockfd,buffer,VEDIOLEN,0);
        if(recvsize<0)
        {
         printf("recv error");
         //exit(1);
 
        }
          
       
        if(strstr(buffer,"Content-Length"))
        {
              begin=strstr(buffer,"Content-Length");
              end=strstr(buffer,"X-Timestamp");
              memcpy(mem,begin+16,end-2-begin-16);
              printf("content-lenth:%d\n",atoi(mem));
        }
        else
        {
                continue;
        }

       //接收视频数据
        memset(buffer,0,VEDIOLEN);
        recvsize=recv(vedio_sockfd,buffer,atoi(mem),0);
        if(recvsize<0)
        {
         printf("recv error");        
        }
      
             
             
              printf("jieshoudao  vedio:%d\n",recvsize);

      //发送到云服务器
        if(sendto(ali_sockfd,buffer,recvsize,0,(const struct sockaddr*)&ali_server,sizeof(ali_server))<0)
        {
            printf("sendto error");
           //exit(1);




        }
      
         printf("send to server\n");
       //接收无关信息
        memset(buffer,0,VEDIOLEN);
        recv(vedio_sockfd,buffer,1024,0);
      




    }

}
